XIncludeFile "GetRequiredSize.pbi"
XIncludeFile "ArrangeGadgets.pbi"

;{
;- Window Constants
;
Enumeration
  #wdwDesigner
EndEnumeration

;- Gadget Constants
;
Enumeration
  #tvGadgets
  #lvAttributes
  #ctrDesign
  #ctrOutput
EndEnumeration

;- MenuBar Constants
;
Enumeration
  #ctxAddItems
EndEnumeration

Enumeration
  #miAddTextGadget
  #miAddStringGadget
  #miAddButtonGadget
  #miAddVerticalBox
  #miAddHortizontalBox
EndEnumeration
;}

Global NewList Control.i()

Procedure LayoutControls()
  Protected offsetX.i, offsetY.i, x.i, y.i
  offsetX = 6
  offsetY = 6
  x = 0
  y = 0
  
  ForEach Control()
    ResizeGadget(Control(), x + offsetX, y + offsetY, GadgetWidth(#ctrOutput) - (offsetX * 2), GetRequiredHeight(Control()))
    
  Next

EndProcedure

Procedure OpenDesignerWindow()
  If OpenWindow(#wdwDesigner, 216, 0, 758, 579, "New window ( 0 )",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreatePopupMenu(#ctxAddItems)
      OpenSubMenu("Add Gadgets")
      MenuItem(#miAddTextGadget, "Text Gadget")
      MenuItem(#miAddStringGadget, "String Gadget")
      MenuItem(#miAddButtonGadget, "Button Gadget")
      CloseSubMenu()

      MenuBar()

      OpenSubMenu("Add Layout")
      MenuItem(#miAddVerticalBox, "Box - Vertically Aligned")
      MenuItem(#miAddHortizontalBox, "Box - Horizontally Aligned")
      CloseSubMenu()
    EndIf


    TreeGadget(#tvGadgets, 10, 10, 200, 170)
    AddGadgetItem(#tvGadgets, -1, "Window", 0, 0) 
    
    ListViewGadget(#lvAttributes, 10, 190, 200, 380)
    
    ContainerGadget(#ctrOutput, 220, 10, 530, 560, #PB_Container_Flat)
    CloseGadgetList()
  EndIf
EndProcedure


OpenDesignerWindow()

Repeat
  Select WaitWindowEvent()     ; check for window events
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #tvGadgets
          Select EventType()
            Case #PB_EventType_RightClick
              DisplayPopupMenu(#ctxAddItems, WindowID(#wdwDesigner))  ; now display the popup-menu
          EndSelect
      EndSelect
      
    Case #PB_Event_Menu        ; an item of the popup-menu was clicked
      Select EventMenu()     ; get the clicked menu item...
        Case #miAddButtonGadget
          OpenGadgetList(#ctrOutput)
          AddElement(Control())
          Control() = ButtonGadget(#PB_Any, 0, 0, 0, 0, "Button")
          LayoutControls()
          CloseGadgetList()
        Case #miAddStringGadget
        Case #miAddTextGadget
        
        Case #miAddVerticalBox
        Case #miAddHortizontalBox
      EndSelect
    
    Case #PB_Event_CloseWindow
      Quit = 1
  EndSelect
Until Quit = 1

; IDE Options = PureBasic 4.60 Beta 3 (Windows - x86)
; CursorPosition = 1
; Folding = +
; EnableXP